package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zzw
 * @date 2025/5/10
 * @description 订单管理
 */
@Api(tags = "订单管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO 订单搜索条件
     * @return Result<PageResult < OrderVO>>
     */
    @ApiOperation("订单分页条件查询")
    @GetMapping("/conditionSearch")
    public Result<PageResult<OrderVO>> conditionSearch(
            @ApiParam("订单分页查询条件") OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("分页查询订单：{}", ordersPageQueryDTO);
        PageResult<OrderVO> pageResult = orderService.page(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询订单详情
     *
     * @param id 订单id
     * @return Result<OrderVO>
     */
    @ApiOperation("根据ID查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> getDetailsById(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        log.info("根据id查询订单详情：{}", id);
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 订单取消
     *
     * @param ordersCancelDTO 订单取消参数
     * @return Result<String>
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result<String> cancel(@ApiParam("订单取消参数") @RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单：{}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return Result<OrderStatisticsVO>
     */
    @ApiOperation("订单状态统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getStatistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.getOrderStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 订单完成
     *
     * @param id 订单id
     * @return Result<String>
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result<String> complete(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 订单拒绝
     *
     * @param rejectionDTO 订单拒绝参数
     * @return Result<String>
     */
    @ApiOperation("拒绝订单")
    @PutMapping("/rejection")
    public Result<String> reject(@ApiParam("订单拒绝参数") @RequestBody OrdersRejectionDTO rejectionDTO) {
        orderService.reject(rejectionDTO);
        return Result.success();
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO 订单确认参数
     * @return Result<String>
     */
    @ApiOperation("确认接单")
    @PutMapping("/confirm")
    public Result<String> confirm(@ApiParam("订单id") @RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id 订单id
     * @return Result<String>
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result<String> delivery(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        orderService.delivery(id);
        return Result.success();
    }
}
